package com.fei.spring.rabbitmq.two;


import com.fei.spring.rabbitmq.utils.RabbitMQUtils;
import com.fei.spring.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.*;


//消息在手动应答时是不允许不丢失的，并且放回队列中重新消费
public class Work03 {
    //队列名称
    public static final String TASK_QUEUE_NAME = "ack_queue";
    //接收消息
    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("C1等待接收消息处理时间1S");
        //睡眠1秒

        DeliverCallback deliverCallback = (String consumerTag, Delivery message) -> {
            SleepUtils.sleep(1);
            System.out.println("接收到的消息："+new String(message.getBody(),"UTF-8"));
            /*手动应答
             * 1.消息的标记 tag
             * 2.是否批量应答
             **/
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        channel.basicQos(2);
        //消息接收被取消时，执行下面的内容
        CancelCallback callback = (consumerTag) -> {
            System.out.println(consumerTag+ "消费者取消消费接口逻辑");
        };
        //消息接收
        /*
         * 消费者消费消息
         * 1.消费哪个队列
         * 2.消费成功之后是否要自动应答 true自动应答 false手动应答
         * 3.消费者未成功消费的回调
         * 4.消费者取消消费的回调
         * */
        channel.basicConsume(TASK_QUEUE_NAME,false,deliverCallback,callback);
    }
}